import { createSlice } from '@reduxjs/toolkit';

export const counterSlice = createSlice({
  name: 'counter', //命名空间
  initialState: {
    value: 0,
    status: 'idle',
  },
  reducers: {
    //toolkit内部做了一些事情,自动帮我们修正引用变化的数据
    //state.value='xxxx'
    // object.assign({},state,{
    //     value:"xxxx",
    // })
    //相加
    increment: (state) => {
      state.value += 1;
    },
    //相减
    decrement: (state) => {
      state.value -= 1;
    },
    incrementByAmount: (state, action) => {
      state.value += action.payload;
    },
  },
});

//定义action
export const { increment, decrement, incrementByAmount } = counterSlice.actions;

//方便取数据
export const selectCount = (state: any) => state.counter.value;

export default counterSlice.reducer;
